// 工厂方法, 用于创建 一个 store module
import moduleCreator from '../moduleCreator'

import api from '@/api'
import { parseTime } from '@/utils'

// moduleName 指向了
const modelName = 'purchase.order'

const Model = api.env(modelName)

const baseModel = {
  async create_by_so(payload) {
    console.log('create_by_so', payload)
    const { so, fields } = payload
    const { name, analytic_account_partner_id, order_line__objects } = so
    const order_line = order_line__objects[0]
    const {
      name: line_name,
      product_id,
      product_template_id,
      product_uom,
      price_unit,
      product_uom_qty
    } = order_line

    const si = await api.env('product.supplierinfo').search_one({
      domain: {
        product_tmpl_id: product_template_id,
        name: analytic_account_partner_id
      },
      fields: { price: null }
    })

    console.log('SI', si)

    const line_values = {
      date_planned: parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}'),
      name: line_name,
      product_id,
      product_uom,
      price_unit: si.price || price_unit,
      product_qty: product_uom_qty,
      qty_received: product_uom_qty
    }

    const values = {
      sale_order_id: so.id,
      origin: name,
      partner_id: analytic_account_partner_id,
      order_line: [[0, 0, line_values]]
    }

    console.log('values,', values)

    const po = await Model.create(values, { fields })
    console.log('po,', po)
    return po
  },
  async action_confirm(payload) {
    const { id, fields } = payload
    const res = await Model.call('button_confirm', [id])
    console.log('action_confirm,', res)
    const res2 = await Model.browse_one(id, { fields })
    console.log('action_confirm,', res2)

    return res2
  },

  async action_done(payload) {
    const { id, fields } = payload
    const res = await Model.call('action_done', [id])
    console.log('action_done,', res)
    const res2 = await Model.browse_one(id, { fields })
    console.log('action_done,', res2)
    return res2
  },

  async create_invoices(payload) {
    const { id, fields } = payload
    // console.log('create_invoices,', id)
    const res1 = await Model.call('_create_invoices', [id])
    console.log('create_invoices,', res1)
    const res = await Model.browse_one(id, { fields })
    console.log('browse_one,', res)
    return res
  }
}

const fields = {
  name: null,
  origin: null,
  state: null,
  date_order: null,
  partner_id: null,
  amount_total: null,

  order_line: {
    product_id: null,
    price_unit: null
  },
  invoice_count: null,
  invoice_ids: {
    name: null,
    amount_total: null,
    state: null,
    invoice_payment_state: null,
    invoice_line_ids: null
  },
  invoice_status: null
}

const config = {
  model: modelName,
  fieldsForSearch: fields,
  fieldsForBrowse: fields
}

const state = {
  invoice: {}
}

const mutations = {
  SET_invoice: (state, row) => {
    state.invoice = row
  }
}

const actions = {
  async after_browse_one({ commit, state, dispatch }, payload) {
    const order = payload
    commit('SET_DataDict', order)
    const invs = order.invoice_ids__objects
    const invoce = invs && invs.length ? order.invoice_ids__objects[0] : {}
    await this.dispatch('AccountMove/setDict', invoce)

    return new Promise(resolve => {
      resolve(order)
    })
  },

  async browse_one({ commit, state, dispatch }, payload) {
    const order = await dispatch('_browse_one', payload)
    return dispatch('after_browse_one', order)
  },

  async find_by_so({ commit, state, dispatch }, payload) {
    const {
      // id,
      name
      // analytic_account_partner_id,
      // order_line__objects: { product_id }
    } = payload

    const order = await Model.search_one({
      domain: { origin: name },
      fields
    })

    console.log('find_by_so', order)
    if (order.id) {
      return dispatch('after_browse_one', order)
    }

    return dispatch('create_by_so', payload)
  },

  async create_by_so({ commit, state, dispatch }, payload) {
    const res = await baseModel.create_by_so({
      so: payload,
      fields
    })

    return dispatch('after_browse_one', res)
  },

  async action_confirm({ commit, state, dispatch }, payload) {
    const res = await baseModel.action_confirm({ ...payload, fields })
    // console.log('action_confirm', res)
    commit('SET_DataDict', res)
    return new Promise(resolve => {
      resolve(res)
    })
  },

  async create_invoices({ commit, state, dispatch }, payload) {
    const res = await baseModel.create_invoices({ ...payload, fields })
    console.log('create_invoices', res)
    return dispatch('after_browse_one', res)
  },

  async action_done({ commit, state, dispatch }, payload) {
    const res = await baseModel.action_done({ ...payload, fields })
    // console.log('action_confirm', res)
    commit('SET_DataDict', res)
    return new Promise(resolve => {
      resolve(res)
    })
  }
}

export default moduleCreator(config, {
  state,
  mutations,
  actions
})

// // 这是最简单的写法
// export default moduleCreator(modelName)

// 如果有扩展, 则按照下面的写法
// const state = {
//   //
// }
// const mutations = {
//   //
// }
// const actions = {
//   //
// }

// export default moduleCreator(modelName, {
//   namespaced: true,
//   state,
//   mutations,
//   actions
// })

// 如果 不继承, 用下面的写法
// const state = {
//   //
// }
// const mutations = {
//   //
// }
// const actions = {
//   //
// }

// export default {
//   namespaced: true,
//   state,
//   mutations,
//   actions
// }
